/**
 * web2010_admin
 * cn.aresoft.web.action.system.user UserAction.java
 */
package com.xsoft.web.action.sys.user;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.xsoft.framework.util.EncryptUtil;
import com.xsoft.framework.util.Page;
import com.xsoft.framework.web.IBasicAction;
import com.xsoft.logic.model.monitor.MonitorServer;
import com.xsoft.logic.model.sys.Menu;
import com.xsoft.logic.model.sys.Module;
import com.xsoft.logic.model.sys.User;
import com.xsoft.logic.model.sys.UserRoleFlag;
import com.xsoft.logic.model.sys.UserRolePrivilege;
import com.xsoft.logic.service.interfaces.monitor.MonitorServerService;
import com.xsoft.logic.service.interfaces.sys.module.ModuleService;
import com.xsoft.logic.service.interfaces.sys.role.RoleService;
import com.xsoft.logic.service.interfaces.sys.user.UserService;
import com.xsoft.web.action.AdminAction;


/**
 * @author 潘冬 2010-8-10 上午10:00:06
 */
public class UserAction extends AdminAction {
	private UserService userService; //也可以加载其他.xml的对应的sql
	private ModuleService moduleService;
	private MonitorServerService monitorServerService;
	private RoleService roleService;
	private static String FoldName = "sys/user";
	private User vo = new User();
	private List<User> userList;
	private List<UserRoleFlag> userRoleList;
	private List<Module> moduleList;
	
	public String login() {
		String username = vo.getName();
		String password = vo.getPassword();
		boolean check = false;
		if ((username == null || username.length() == 0)
				&& (password == null || password.length() == 0)) {
			check = false;
		}

		try {
			vo = userService.selectUserByName(username);
		} catch (Exception e) {
			e.printStackTrace();
			check = false;
		}
		try {
			
			System.out.println(EncryptUtil.encrypt(password));
			System.out.println(vo.getPassword());
			
			if (EncryptUtil.encrypt(password).equals(vo.getPassword())) {
				check = true;
			} else {
				check = false;
			}
		} catch (Exception e) {
			check = false;
		}
		if (check) {
			HashMap map1 =  new HashMap();
			map1.put("Id", vo.getId());
			map1.put("type", "1");
			ArrayList<UserRolePrivilege> privilegeList = (ArrayList<UserRolePrivilege>)roleService.getUserRolePrivilege(map1);
			ArrayList<MonitorServer> monitorServerList = (ArrayList<MonitorServer>)monitorServerService.selectMonServerByType(null);
			
			session.put("user", vo);
			session.put("privilegeList", privilegeList);
			session.put("monitorServerList", monitorServerList);
			return SUCCESS;
		} else {
			this.setMessage("用户或者密码错误");
			this.addActionError("用户或者密码错误");
			setLocalationURL(this.getBasePath()+ IBasicAction.Struts2FilterFold + "/login.jsp");
			return IBasicAction.RedirectionLoaction;
		}
	}
	
	
	public String logout() {
		session.clear();
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	
	
	public String loadLeftMenu() {
		
		User u = (User)session.get("user");
		moduleList = moduleService.getAllModule();
		
		if(!"1".equals(u.getIsRoot())){ //非超级管理员
			
			HashMap hm =  new HashMap();
			hm.put("Id", u.getId());
			hm.put("type", "1");
			ArrayList<UserRolePrivilege> menuRefList = (ArrayList<UserRolePrivilege>)roleService.getUserRolePrivilege(hm);
			if (menuRefList == null || menuRefList.size() == 0) {
				createCommonResult(0,"ok",moduleList);
				return SUCCESS;
			}
			for(Module module : moduleList) {
				List<Menu> menuList =  module.getMenus();
				for(int i=0;i<menuList.size();i++) {
					Menu menu = menuList.get(i);
					for(int j=0;j<menuRefList.size();j++){
						if(menuRefList.get(j).getMenuId().longValue()==menu.getId().longValue()){
							menu.setSelected("1");
						}
					}
				}
			}
		}else{
			for(Module module : moduleList) {
				List<Menu> menuList =  module.getMenus();
				for(int i=0;i<menuList.size();i++) {
					menuList.get(i).setSelected("1");
				}
			}
		}
		createCommonResult(0,"ok",moduleList);
		return SUCCESS;
	}
	
	
	
	public String insert() {
		try {
			vo.setPassword(EncryptUtil.encrypt(vo.getPassword()));
		}catch (Exception e) {
			e.printStackTrace();
		}	
		userService.insert(vo);
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}

	
	
	
	public String select() {
		Page page = userService.getDateByPage(gotoPage, pageSize, vo);
		userList = (List<User>) page.getResult();
		this.setPageInfo(page);
		return SUCCESS;
	}
	
	
	public String beforeSaveRole(){
		 userRoleList = (List<UserRoleFlag>)userService.selectUserRoleByUserId(Long.parseLong(request.getParameter("id")));
		return SUCCESS;
	}
	

	public String delete() {
		Long id = Long.parseLong(request.getParameter("id"));
		userService.delete(id);
		userService.deleteUserRole(id);
		userService.deleteUserPrivilege(id);
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	
	
	
	
	public String go() {
		return SUCCESS;
	}
	
	
	
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public static String getFoldName() {
		return FoldName;
	}


	public static void setFoldName(String foldName) {
		FoldName = foldName;
	}


	public User getVo() {
		return vo;
	}


	public void setVo(User vo) {
		this.vo = vo;
	}


	public List<User> getUserList() {
		return userList;
	}
	

	public List<UserRoleFlag> getUserRoleList() {
		return userRoleList;
	}



	public void setUserRoleList(List<UserRoleFlag> userRoleList) {
		this.userRoleList = userRoleList;
	}



	public ModuleService getModuleService() {
		return moduleService;
	}



	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}



	public RoleService getRoleService() {
		return roleService;
	}



	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}


	public MonitorServerService getMonitorServerService() {
		return monitorServerService;
	}


	public void setMonitorServerService(MonitorServerService monitorServerService) {
		this.monitorServerService = monitorServerService;
	}
		
}
